import random
import sys
import pygame
from pygame import key

from . import game
from . import setup
from . import constants as C
from .info import Info


class Menu:
    def __init__(self):
        self.set_background()  #设置背景
        self.set_player()  #设置玩家
        self.set_cursor()  #设置指针
        self.info = Info("main_menu", 0)  # 载入文字信息
        self.finished = False  # 当前菜单界面状态  菜单 -> 加载 -> 正式游戏
        self.next = "load_screen"  # 下一界面状态
        self.score = 0

    def set_background(self):
        # 设置背景
        self.background = setup.GRAPHICS["bc"]
        self.background_rect = self.background.get_rect()
        self.background = pygame.transform.scale(
            self.background, (int(self.background_rect.width * C.BG_MULTI),
                              int(self.background_rect.height * C.BG_MULTI)))
        # 设置视窗
        self.viewport = setup.SCREEN.get_rect()
        # 设置标题
        self.title = game.get_image(setup.GRAPHICS["title"], 168, 192, 281,
                                    119, (233, 20, 99), C.BG_MULTI)

    def set_player(self):
        pass

    def set_cursor(self):
        # 20,60->380,435
        # 起点坐标(20，60),长宽(360,375)

        self.cursor = pygame.sprite.Sprite()
        print("比例", C.CS_MULTI)
        self.cursor.image = game.get_image(setup.GRAPHICS["snake_cursor"], 20,
                                           60, 360, 375, (255, 255, 255),
                                           C.CS_MULTI)
        rect = self.cursor.image.get_rect()
        rect.x, rect.y = (220, 365)
        self.cursor.rect = rect
        # 光标当前状态
        self.cursor.state = "1"

    def update_cursor(self, keys):
        if keys[pygame.K_UP]:
            self.cursor.state = "1"
            print("up")
            self.cursor.rect.y = 365

        elif keys[pygame.K_DOWN]:
            print("down")
            self.cursor.state = "2"
            self.cursor.rect.y = 410
        elif keys[pygame.K_RETURN]:  # 按下回车
            if self.cursor.state == "1":  # 进入匹配界面
                self.finished = True
            elif self.cursor.state == "2":  # 退出游戏
                self.finished = True
                sys.exit()

    def update(self, surface, keys):
        self.update_cursor(keys)
        surface.blit(self.background, self.viewport)
        surface.blit(self.title, (189, 100))
        surface.blit(self.cursor.image, self.cursor.rect)

        # self.info.update()
        self.info.draw(surface)
